﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AzureGames.Data.InMemory
{
    public class UserRepository : IUserRepository
    {
        object _lock = new object();
        static Dictionary<string, Model.User> Users = DefaultUsers();

        private static Dictionary<string, Model.User> DefaultUsers()
        {
            var user1 = new Guid("DF2AA2FD-6AA8-474F-B29B-BCCD8247D397").ToString();
            var user2 = new Guid("2F851A61-249B-4942-9FE4-2276D39183EF").ToString();
            var user3 = new Guid("328C8086-B7FB-4F65-A0E2-6F2AE40E1300").ToString();            

            return new Dictionary<string, Model.User>
                       {
                           { 
                               user1, 
                               new Model.User(user1, "XKRfKaACuPfmy11bcHq2QlCAcglHi5JbrjhtVl8R/Xs=", "Charlotte", "C", "charlotte@gmail.com", "cc") 
                                                { CreatedDate = new DateTime(2012,01,01), LastUpdatedDate = new DateTime(2012,01,01) }},
                           { 
                               user2, 
                               new Model.User(user2, "UWxfy1ywq4yVw9h2Ud3fmGw7GmJ3XH3Ul4QxWTwRGFw=", "Jeffrey", "Chilberto", "jchilberto@hotmail.co.uk", "Saebel")
                                                { CreatedDate = new DateTime(2012,01,01), LastUpdatedDate = new DateTime(2012,01,01), LoginType="uri:WindowsLiveID" }}, 
                           { 
                               user3, 
                               new Model.User(user3, "https://www.google.com/accounts/o8/id?id=AItOawnpQapZYgfptJhvBfhZmulcbqPg0mxZcUA", "Joe", "Tester1", "chilberto@yahoo.com", "OnionHead")
                                                { CreatedDate = new DateTime(2012,01,01), LastUpdatedDate = new DateTime(2012,01,01), LoginType="Google" }}                           
                       };
        }


        public Model.User GetUser(string userID)
        {
            var user = Users[userID];

            return user;
        }

        


        public Model.User LoginUser(string nameIdentifier)
        {
            var user = Users.Where(n => n.Value.NameIdentifier == nameIdentifier).SingleOrDefault().Value;

            if (user != null)
            {
                user.LastLogin = DateTime.Now;
            }
            return user;
        }


        public void AddUser(Model.User user)
        {
            lock (_lock)
            {
                Users[user.ID] = user;
            }
        }


        public void UpdateUser(Model.User updatedUser)
        {
            lock (_lock)
            {
                var user = Users[updatedUser.ID];

                if (user != null)
                {
                    user.AvatarImage = updatedUser.AvatarImage;
                    user.Email = updatedUser.Email;
                    user.FirstName = updatedUser.FirstName;
                    user.LastName = updatedUser.LastName;
                    user.LastUpdatedDate = DateTime.Now;
                    user.Name = updatedUser.Name;
                }
            }
        }


        public List<Model.User> GetUsers()
        {
            return Users.Values.ToList();        
        }
    }
}
